<template>
  <div class="loginbody">
    <div class="loginbox">
      <div class="head">
        <img src="../assets/yier.jpg" />
      </div>
      <el-form ref="loginRef" :rules="loginRules" :model="loginForm" label-width="80px" class="form">
        <el-form-item label="userName" prop="username">
          <el-input v-model="loginForm.username" type="text" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item label="password" prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item class="but">
          <el-button type="primary" @click="login">登录</el-button>
           <el-button type="infor" @click="resetLoginForm">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        user: new FormData(),
        loginForm:{
          username: '',
          password: ''
        },
        loginRules:{
          username:[
            {required:true, message:"请输入用户名称",trigger:"blur"},
            {min:3,max:10,message:"长度在3到5个字符",trigger:"blur"}
          ],
          password:[
            {required:true,message:"请输入登录密码",trigger:"blur"},
            {min:6,max:15,message:"长度在6到15个字符",trigger:"blur"}
          ]
        }
      }
    },
    methods:{
      resetLoginForm() {
        this.$refs.loginRef.resetFields();
      },
      login() {
        this.$refs.loginRef.validate(async valid=>{
          this.user.append("username",this.loginForm.username);
          this.user.append("password",this.loginForm.password);
          if(!valid) return;
          const res =await this.$http.post("login",this.user)
          if(res.status!=200) return this.$message.error('登录失败！'+res.status);
          this.$message.success('登录成功！'+res.status);
          window.sessionStorage.setItem("token",res.token);
          this.$router.push("/home");
        });
      }
    }
  }
</script>

<style lang="less" scoped>
.loginbody{
  background-color: orange;
  height: 100%;
}
.loginbox{
  width: 450px;
  height: 300px;
  background-color: white;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
.head{
  height: 60px;
  width: 60px;
  border: 1px solid white;
  border-radius: 50%;
  box-shadow: 0 0 6px #888;
  position: absolute;
  left: 50%;
  transform: translate(-50%);
   margin-top: 30px;
  img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}

.form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.but{
    display: flex;
    justify-content: flex-end;
  }
</style>
